﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.GERP.SECURITY.DAL;
using Telerik.Web.UI;

namespace KPIS.GERP.SECURITY.App_UI.UserControls
{
    public partial class Approver : System.Web.UI.UserControl
    {
        ApprovePathDAL app = new ApprovePathDAL();
        SysExecPosGroupDAL exe = new SysExecPosGroupDAL();
        HrmPositionDAL pos = new HrmPositionDAL();
        private SsMng ssMng = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rblApproverType_SelectedIndexChanged(null, null);
            }

            LoadDDLExecPosGroupSeq();
            LoadDDLPosSeq();
        }

        public void LoadData(DataRow dr)
        {
            lblApprovePathSeq.Text = dr["approve_path_seq"].ToString();
            rblApproverType.SelectedValue = dr["approver_type"].ToString();
            rblApproverType_SelectedIndexChanged(null, null);
            lblApproverType.Text = rblApproverType.SelectedItem.Text;

            if (PanelType1.Visible)
            {
                ddlExecPosGroupSeq.SelectedValue = dr["exec_pos_group_seq"].ToString();
                lblExecPosGroupSeq.Text = dr["exec_pos_group_name"].ToString();
            }
            else
            {
                ddlPosSeq.SelectedValue = dr["pos_seq"].ToString();
                lblPosSeq.Text = dr["pos_text"].ToString();
            }
        }

        public void SetControl(bool bEdit)
        {
            cmdEdit.Visible = !bEdit;
            cmdOK.Visible = bEdit;
            cmdMoveLeft.Visible = bEdit;
            cmdMoveRight.Visible = bEdit;

            lblApproverType.Visible = !bEdit;
            lblExecPosGroupSeq.Visible = !bEdit;
            lblPosSeq.Visible = !bEdit;

            rblApproverType.Visible = bEdit;
            ddlExecPosGroupSeq.Visible = bEdit;
            ddlPosSeq.Visible = bEdit;
        }

        protected void LoadDDLExecPosGroupSeq()
        {
            ddlExecPosGroupSeq.DataSource = exe.GetFull();
            ddlExecPosGroupSeq.DataBind();

            ddlExecPosGroupSeq.Items.Insert(0, new RadComboBoxItem("< เลือก >", ""));
        }

        protected void LoadDDLPosSeq()
        {
            ddlPosSeq.DataSource = pos.GetFull(1);
            ddlPosSeq.DataBind();

            ddlPosSeq.Items.Insert(0, new RadComboBoxItem("< เลือก >", ""));
        }

        public void SetStepNo(int nStepNo)
        {
            lblStepNo.Text = nStepNo.ToString();
        }

        protected void rblApproverType_SelectedIndexChanged(object sender, EventArgs e)
        {
            PanelType1.Visible = rblApproverType.SelectedValue == "1";
            PanelType2.Visible = rblApproverType.SelectedValue == "2";
        }

        protected void cmdOK_Click(object sender, ImageClickEventArgs e)
        {
            if (PanelType1.Visible && ddlExecPosGroupSeq.SelectedValue.Length < 0)
            {
                lblError1.Visible = true;
                return;
            }
            else if (PanelType2.Visible && ddlPosSeq.SelectedValue.Length < 0)
            {
                lblError2.Visible = true;
                return;
            }
            else
            {
                lblError1.Visible = false;
                lblError2.Visible = false;
            }

            string strApprovePathSeq = lblApprovePathSeq.Text;
            string strProcess = Session["Process"].ToString();
            int nStepNo = Convert.ToInt32(lblStepNo.Text);
            string strApproverType = rblApproverType.SelectedValue;
            Nullable<int> nExecPosGroupSeq = null;
            Nullable<int> nPosSeq = null;
            int nEmpSeq = ssMng.EmpSeqI;// Convert.ToInt32(Session["EmpSeq"]);

            if (PanelType1.Visible)
            {
                nExecPosGroupSeq = Convert.ToInt32(ddlExecPosGroupSeq.SelectedValue);
            }
            else
            {
                nPosSeq = Convert.ToInt32(ddlPosSeq.SelectedValue);
            }

            if (strApprovePathSeq.Length == 0)
            {
                app.Insert(strProcess, nStepNo, strApproverType, nExecPosGroupSeq, nPosSeq, nEmpSeq);
            }
            else
            {
                app.Update(Convert.ToInt32(strApprovePathSeq), strProcess, nStepNo, strApproverType
                    , nExecPosGroupSeq, nPosSeq, nEmpSeq);
            }

            ((ApprovePath)Page).LoadStep();
            SetControl(false);
        }

        protected void cmdEdit_Click(object sender, ImageClickEventArgs e)
        {
            SetControl(true);
        }

        protected void cmdMoveLeft_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void cmdMoveRight_Click(object sender, ImageClickEventArgs e)
        {

        }
    }
}